package csc.fresher.finalgroupfour.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Customer;
import csc.fresher.finalgroupfour.domain.User;
import csc.fresher.finalgroupfour.dto.CustomerDTO;
import csc.fresher.finalgroupfour.service.AccountService;
import csc.fresher.finalgroupfour.service.CustomerService;
import csc.fresher.finalgroupfour.service.UserService;

@Controller
public class ProfileController {
	@Autowired
	private UserService userService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private CustomerService customerService;

	/**
	 * Render for user's profile view.
	 */
	@RequestMapping(value = "/viewProfile", method = RequestMethod.GET)
	public ModelAndView viewProfile(HttpServletRequest request) {
		String error = request.getParameter("error");
		String validate = request.getParameter("validate");
		ModelAndView model = new ModelAndView("viewProfile");
		
		// Check when user change password and return to this function.
		if (error == null) {
			model.addObject("passwordMessage", "");
		} else if (error.equals("true")) {
			if (validate == null) {
				model.addObject("passwordMessage", "Current password mismatch!");
			} else if (validate.equals("lt8")) {
				model.addObject("passwordMessage", "New password must has more than 8 characters.");
			} else if (validate.equals("gt20")) {
				model.addObject("passwordMessage", "New password must has less than 20 characters.");
			}
		} else if (error.equals("false")) {
			model.addObject("passwordMessage", "Password changed successfully!");
		}
		
		String loginId = userService.getLoginId();
		User user = userService.findUser(loginId);
		Customer customer = customerService.findCustomerByUser(user);

		model.addObject("user", user);
		model.addObject("customer", customer);
		return model;
	}
	
	@RequestMapping(value = "/adminViewProfile",  produces="application/json")
	public @ResponseBody CustomerDTO viewProfileAdmin(@RequestParam("account") String accountNumber) {
		Account account = accountService.findAccount(accountNumber);
		Customer customer = account.getCustomer();
		
		CustomerDTO cusDTO = new CustomerDTO(customer);
		return cusDTO;
	}

	/**
	 * Form checking and modifying profile.
	 */
	@RequestMapping(value = "/changeInfo")
	public String getProfile(@ModelAttribute("customer") Customer customer) {
		customerService.updateCustomer(customer);
		return ("redirect:/viewProfile");
	}

	/**
	 * Form change user's password.
	 */
	@RequestMapping(value = "/changePass")
	public String changePass(@RequestParam("oldpass") String oldpass, 
			@RequestParam("newpass") String newpass, Model model) {
		// Get authenticated user.
		String loginId = userService.getLoginId();
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		User user = userService.findUser(loginId);
		if (encoder.matches(oldpass, user.getPassword())) {
			if (newpass.length() < 8) {
				return "redirect:/viewProfile?error=true&validate=lt8";
			} else if (newpass.length() > 20) {
				return "redirect:/viewProfile?error=true&validate=gt20";
			}
			
			String passwordEncoded = encoder.encode(newpass);
			user.setPassword(passwordEncoded);
			userService.updateUser(user);
			return "redirect:/viewProfile?error=false";
		} else {
			return "redirect:/viewProfile?error=true";
		}
	}
}
